// toilet.js
const cityData = require('../../data/data.js')

Page({
  data: {
    cityName: '',
    cityPinyin: '',
    subwayData: null,
    selectedLineIndex: 0,
    selectedLine: null,
    hasSubwayData: false,
    isCollapsed: false  // 控制左右栏目折叠状态
  },

  onLoad(options) {
    // 接收从首页传递的参数
    const { city, cityName } = options
    
    console.log('接收到的参数:', { city, cityName })
    
    this.setData({
      cityPinyin: city || '',
      cityName: cityName || ''
    })

    // 加载数据文件
    this.loadCityData(city)

    // 设置页面标题
    wx.setNavigationBarTitle({
      title: `${cityName || '未知城市'} - 地铁厕所`
    })
  },

  // 加载城市数据
  loadCityData(cityPinyin) {
    console.log('开始加载城市数据:', cityPinyin)
    console.log('可用城市数据:', cityData)
    
    // 查找当前城市的数据
    const currentCityData = cityData.find(city => city.cityPinyin === cityPinyin)
    
    if (currentCityData) {
      console.log('找到城市数据:', currentCityData)
      this.setData({
        subwayData: currentCityData,
        selectedLine: currentCityData.lines[0],
        selectedLineIndex: 0,
        hasSubwayData: true
      })
    } else {
      console.log('未找到城市数据:', cityPinyin)
      this.setData({
        hasSubwayData: false
      })
    }
  },

  // 选择地铁线路
  selectLine(e) {
    const index = e.currentTarget.dataset.index
    const line = this.data.subwayData.lines[index]
    
    console.log('选择线路:', line)
    
    this.setData({
      selectedLineIndex: index,
      selectedLine: line
    })
  },

  // 切换折叠状态（点击右侧面板时）
  toggleCollapse() {
    this.setData({
      isCollapsed: !this.data.isCollapsed
    })
    console.log('切换折叠状态:', this.data.isCollapsed)
  },

  // 展开面板（点击左侧面板时恢复原状态）
  expandPanels() {
    if (this.data.isCollapsed) {
      this.setData({
        isCollapsed: false
      })
      console.log('恢复展开状态')
    }
  },

  // 获取设施标签样式
  getFacilityClass(facility) {
    if (facility === '无障碍设施') return 'facility-accessible'
    if (facility === '母婴室') return 'facility-baby'
    return 'facility-basic'
  },

  onShow() {
    // 页面显示时的逻辑
    console.log('toilet页面显示')
  },

  // 返回首页
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },

  onShareAppMessage() {
    return {
      title: `${this.data.cityName}地铁厕所`,
      path: `/pages/toilet/toilet?city=${this.data.cityPinyin}&cityName=${this.data.cityName}`
    }
  }
}) 